package Vista;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import VO.ProveedorVO;

import conexion.ProxyIR;




/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BajaProveedorGUI extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1968539210564223121L;
	private JLabel codigoLbl;
	private JTextField codigoTxt;
	private JButton salirBtn;
	private JButton eliminarBtn;
	private JButton buscarBtn;
	static private BajaProveedorGUI instance;
	private ProxyIR server;
	private JScrollPane itemsScroll;
	private JLabel mensaje;
	private JTable itemsTbl;
	DefaultTableModel proveedoresTblModel;

	/**
	* Auto-generated main method to display this JFrame
	*/
	static public BajaProveedorGUI getInstance() {
		if (instance == null) {			
			instance = new BajaProveedorGUI();
		}
		
		return instance;
	}
	
	public BajaProveedorGUI() {
		super();
		server = ProxyIR.getInstance();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Baja Proveedor");
			{
				codigoLbl = new JLabel();
				getContentPane().add(codigoLbl);
				codigoLbl.setText("Cuit Proveedor:");
				codigoLbl.setBounds(38, 30, 126, 28);
			}
			{
				codigoTxt = new JTextField();
				getContentPane().add(codigoTxt);
				codigoTxt.setBounds(176, 31, 122, 28);
			}
			{
				eliminarBtn = new JButton();
				getContentPane().add(eliminarBtn);
				eliminarBtn.setText("Eliminar");
				eliminarBtn.setBounds(88, 205, 112, 28);
				eliminarBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						
						String codigo = codigoTxt.getText();
						if(codigo == null){
							mensaje.setText("El cuit esta vacio");
						}else{							
							try {
								ProveedorVO prov = new ProveedorVO();
								prov.setCuit(codigo);
								server.bajaProveedor(prov);
								setItemsModel();
								mensaje.setText("Proveedor borrado exitosamente");
							
								
							} catch (RemoteException e) {
								mensaje.setText("Error al borrar el Proveedor");
								e.printStackTrace();
							}
						}
						codigoTxt.setText("");
						proveedoresTblModel = new DefaultTableModel(getColumnNames(), 0);		
						itemsScroll.setViewportView(itemsTbl);
						itemsTbl.setModel(proveedoresTblModel);
					}

					private void setItemsModel() {
						proveedoresTblModel = new DefaultTableModel();						
					}
				});
			}
			{
				salirBtn = new JButton();
				getContentPane().add(salirBtn);
				getContentPane().add(getBuscarBtn());
				getContentPane().add(getItemsScroll());
				getContentPane().add(getMensaje());
				salirBtn.setText("Salir");
				salirBtn.setBounds(227, 205, 112, 28);
				salirBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {					
						
						BajaProveedorGUI.getInstance().setVisible(false);
						
					}
				});
			}
			pack();
			this.setSize(441, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JButton getBuscarBtn() {
		if(buscarBtn == null) {
			buscarBtn = new JButton();
			buscarBtn.setText("Buscar");
			buscarBtn.setBounds(319, 31, 78, 28);
			buscarBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					String codigo = codigoTxt.getText();
					
					try {
						getItemsModel(server.buscarProveedor(new ProveedorVO(codigo)));
					} catch (RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}					
				}
			});
		}
		return buscarBtn;
	}
	
	private JScrollPane getItemsScroll() {
		if(itemsScroll == null) {
			itemsScroll = new JScrollPane();
			itemsScroll.setBounds(20, 101, 384, 82);
			itemsScroll.setViewportView(getItemsTbl());
		}
		return itemsScroll;
	}
	
	private JTable getItemsTbl() {
		if(itemsTbl == null) {
			proveedoresTblModel = new DefaultTableModel(getColumnNames(), 0);			
			itemsTbl = new JTable();
			itemsScroll.setViewportView(itemsTbl);
			itemsTbl.setModel(proveedoresTblModel);
		}
		return itemsTbl;
	}

	public Vector<String> getColumnNames(){		
		Vector<String> columnNames = new Vector<String>();		
		columnNames.add("Cuit");
		columnNames.add("Nombre");
		columnNames.add("Direccion");		
		return columnNames;		
	}

	public DefaultTableModel getTableModel(){
		return proveedoresTblModel;
	}

	protected void getItemsModel(ProveedorVO proveedor) {		
				
		//proveedoresTblModel = new DefaultTableModel(getColumnNames(), 0);	
		if(proveedor != null && proveedor.getCuit() != null){
			String[] elem = new String[3]; 
			elem[0] = proveedor.getCuit().toString();
			elem[1] = proveedor.getNombre();		
			elem[2] = proveedor.getDireccion();
			getTableModel().addRow(elem);	
		}		
	}	
	
	private JLabel getMensaje() {
		if(mensaje == null) {
			mensaje = new JLabel();
			mensaje.setBounds(38, 76, 359, 19);
		}
		return mensaje;
	}
}
